/*
 * Copyright (C) 2013 Peng fei Pan <sky@xiaopan.me>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jevons.muffin.adapter;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

/**
 * FragmentPagerAdapter的基本封装类
 * @author linaisheng
 * Create at 2019/3/25
 */
public class FragmentListPagerAdapter extends FragmentPagerAdapter {

    private FragmentManager fragmentManager;
    private List<Fragment> fragments;

    /**
     * 构造函数
     *
     * @param fragmentManager FragmentManager
     * @param fragments       List<Fragment>
     */
    public FragmentListPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
        super(fragmentManager);
        this.fragmentManager = fragmentManager;
        this.fragments = fragments;
    }

    /**
     * 构造函数
     *
     * @param fragmentManager FragmentManager
     * @param fragments       Fragment... fragments
     */
    public FragmentListPagerAdapter(FragmentManager fragmentManager, Fragment... fragments) {
        super(fragmentManager);
        this.fragmentManager = fragmentManager;
        this.fragments = new ArrayList<Fragment>(fragments.length);
        for (Fragment fragment : fragments) {
            this.fragments.add(fragment);
        }
    }

    /**
     * 当前位置的Fragment
     *
     * @param position 位置
     * @return Fragment
     */
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    /**
     * Fragment的数量
     *
     * @return int
     */
    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    /**
     * 获取当前页面的title
     *
     * @param position 位置
     * @return CharSequence标题
     */
    @Override
    public CharSequence getPageTitle(int position) {

        Fragment fragment = fragments.get(position);
        if (fragment instanceof GetPageTitleListener) {
            return ((GetPageTitleListener) fragment).onGetPageTitle(position);
        } else {
            return super.getPageTitle(position);
        }
    }

    /**
     * 获取所有的Fragment
     *
     * @return List<Fragment>
     */
    public List<Fragment> getFragments() {
        return fragments;
    }

    /**
     * 设置Fragmnet集合
     *
     * @param fragmentsList Fragment集合
     */
    public void setFragments(List<Fragment> fragmentsList) {
        if (fragments != null && fragments.size() > 0) {
            FragmentTransaction ft = fragmentManager.beginTransaction();
            for (Fragment f : this.fragments) {
                ft.remove(f);
            }
            ft.commit();
            ft = null;
            fragmentManager.executePendingTransactions();
        }

        this.fragments = fragmentsList;
    }

    /**
     * Fragment--Title监听
     */
    public interface GetPageTitleListener {
        /**
         * title回调
         *
         * @param position 当前page的position
         * @return CharSequence
         */
        CharSequence onGetPageTitle(int position);
    }

}